﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using Data;

namespace Bussiness {
    public class PhieuNhapService {
        public string CreateMaPhieuNhap() {
            PhieuNhapRepository repository = new PhieuNhapRepository();
            return repository.CreateMaPhieuNhap();
        }

        /// <summary>
        /// thêm dữ liệu trên phiếu nhập vào csdl
        /// </summary>
        /// <param name="phieuNhap">phiếu nhập</param>
        /// <param name="chiTietPhieuNhap">danh sách chi tiết</param>
        /// <returns></returns>
        public bool AddData(PhieuNhapDTO phieuNhap, List<ChiTietPhieuNhapDTO> chiTietList) {
            //insert phieu nhap
            int phieuNhapID = this.InsertPhieuNhap(phieuNhap);
            //insert chi tiet
            //1. add id phieu nhap
            foreach (ChiTietPhieuNhapDTO item in chiTietList) {
                item.IDPhieuNhap = phieuNhapID;
            }
            //2.insert
            this.InsertChiTietPhieuNhap(chiTietList);


            //tinh toan lai so luong ton


            return true;
        }

        /// <summary>
        /// them phieu nhap
        /// </summary>
        /// <param name="phieuNhap"></param>
        /// <returns></returns>
        private int InsertPhieuNhap(PhieuNhapDTO phieuNhap) {
            PhieuNhapRepository res = new PhieuNhapRepository();
            return res.InsertPhieuNhap(phieuNhap);
        }

        /// <summary>
        /// them chi tiet
        /// </summary>
        /// <param name="chiTietList"></param>
        /// <returns></returns>
        private bool InsertChiTietPhieuNhap(List<ChiTietPhieuNhapDTO> chiTietList) {
            ChiTietPhieuNhapService ct = new ChiTietPhieuNhapService();
            ct.InsertChiTietPhieuNhap(chiTietList);
            return true;
        }
    }
}
